/**    
 * @{#} MyFollowingDoctorActivity.java Create on 2014-7-8 下午2:49:08    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.skinapp.R;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.evan.common.pullrefresh.PullToRefreshBase;
import com.evan.common.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.evan.common.pullrefresh.PullToRefreshListView;
import com.pifuke.adapter.SampleUserAdapter;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.net.UserGetTask;
import com.smiier.skin.net.UserGetTask.UserGetRequest;
import com.smiier.skin.net.UserGetTask.UserGetResponse;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.net.entity.UserWrapper;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class OtherDoctorGuanzhuActivity extends BasicActivity implements
		OnRefreshListener<ListView>, OnItemClickListener {

	private PullToRefreshListView list_following_doctor;
	private TextView text_empty;
	private SampleUserAdapter doctorAdapter;
	String start_qid = "";
	HashMap<String, Object> params = new HashMap<String, Object>(),
			params_ = new HashMap<String, Object>();
	private ArrayList<Object> array = new ArrayList<Object>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myfollowing_doctor);

		init();
		setNavTitle("关注");
		text_empty = (TextView) findViewById(R.id.text_empty);

		list_following_doctor = (PullToRefreshListView) findViewById(R.id.list_myfollowing);
		list_following_doctor.setOnRefreshListener(this);
		list_following_doctor.setPullRefreshEnabled(false);
		list_following_doctor.setScrollLoadEnabled(true);
		doctorAdapter = new SampleUserAdapter();
		list_following_doctor.setAdapter(doctorAdapter);
		try {
			JSONObject doctorInfo = new JSONObject(getIntent().getStringExtra(
					Constant.IDENTITY_KEY));

			params.put(Constant.PARAM_API, "Follow.GetFollowersList");
			params.put("pageSize", Constant.pageSize);
			params.put(Constant.PARAM_TOKEN, GlobalSettings.sToken);
			params.put("following_id", doctorInfo.getInt("Uid"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		params_.put(Constant.PARAM_API, "User.Get");
		list_following_doctor.setOnItemClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
//		MobclickAgent.onResume(this);
		onPullDownToRefresh(null);
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		start_qid = "";
		array.clear();
		loadData(true);
	}

	@Override
	public void onPullUpToLoadMore(PullToRefreshBase<ListView> refreshView) {
		loadData(false);
	}

	public void loadData(boolean isShowDialog) {
		params.put("startuid", start_qid);
		RequestTaskIntercept.requestIntercept(activity, Constant.URL.REQUEST,
				new RequestHandler(activity, new HandlerCallback() {

					@Override
					public void callback(Object o) {
						if (!CommonUtility.isNull(o)) {
							try {
								JSONObject object = (JSONObject) o;
								if (CommonUtility.response200(object)) {
									object = object.getJSONObject("Res");
									JSONArray arr = object
											.getJSONArray("Follows");
									String ids = "";
									ArrayList<Long> uids = new ArrayList<Long>();
									for (int i = 0; i < arr.length(); i++) {
										object = arr.getJSONObject(i);
										ids += object.getString("Follower_Uid");
										uids.add(object.getLong("Follower_Uid"));
										if (i < arr.length() - 1) {
											ids += ",";
										}
									}
									if (arr.length() > 0) {
										start_qid = arr.getJSONObject(
												arr.length() - 1).getString(
												"Follower_Uid");
									}
									if (!CommonUtility.isNull(ids)) {
										params_.put("uids", ids);
										UserGetTask task = new UserGetTask();
										UserGetRequest req = new UserGetRequest();

										req.uids = uids;
										task.addListener(new NetTaskListener<UserGetTask.UserGetRequest, UserGetTask.UserGetResponse>() {

											@Override
											public void onException(
													INetTask<UserGetRequest, UserGetResponse> task,
													Exception e) {
												list_following_doctor
														.setContextEmptyType(
																true, 2);
											}

											@Override
											public void onComplete(
													INetTask<UserGetRequest, UserGetResponse> task,
													UserGetResponse response) {
												list_following_doctor
														.doComplete();
												if (response != null
														&& response.Res.size() > 0) {
													ArrayList<UserWrapper> wrapper = new ArrayList<UserWrapper>();
													for (User u : response.Res) {
														UserWrapper wrap = new UserWrapper();
														wrap.User = u;
														wrap.Uid = u.Uid;
														wrapper.add(wrap);
													}
													doctorAdapter
															.setIsDoctor(false);
													doctorAdapter
															.setDataProvider(wrapper);
													doctorAdapter
															.notifyDataSetChanged();
													list_following_doctor
															.setHasMoreData(false);
													;
													list_following_doctor
															.setPullLoadEnabled(false);
													list_following_doctor
															.setPullRefreshEnabled(false);
													list_following_doctor
															.setContextEmptyType(
																	false, 1);
												} else {
													list_following_doctor
															.setContextEmptyType(
																	true, 1);
												}

											}
										});
										task.setRequest(req);
										add(task);
										/*
										 * RequestTaskIntercept.requestIntercept(
										 * activity, Constant.URL.REQUEST, new
										 * RequestHandler(activity, new
										 * HandlerCallback() {
										 * 
										 * @Override public void callback(
										 * Object o) { if (!CommonUtility
										 * .isNull(o)) { list_following_doctor
										 * .doComplete(); try { JSONObject
										 * object = (JSONObject) o; JSONArray
										 * user_array = object
										 * .getJSONArray(Constant
										 * .JSON_PARAM_RES); if (user_array
										 * .length() > 0) { CommonUtility
										 * .putAll(array, user_array);
										 * list_following_doctor
										 * .setHasMoreData(true); } else {
										 * list_following_doctor
										 * .setHasMoreData(false); }
										 * 
										 * if (CommonUtility
										 * .isNull(doctorAdapter)) {
										 * doctorAdapter = new UserAdapter(
										 * OtherDoctorGuanzhuActivity.this,
										 * array, false);
										 * 
										 * list_following_doctor
										 * .setAdapter(doctorAdapter); } else {
										 * doctorAdapter .notifyUpdate(); } }
										 * catch (Exception e) {
										 * e.printStackTrace(); } } } }), false,
										 * params_);
										 */
									} else {
										if (array.size() < 1) {
											text_empty
													.setVisibility(View.VISIBLE);
										} else {
											list_following_doctor
													.setHasMoreData(false);
										}

										list_following_doctor.doComplete();
									}
								} else {
									CommonUtility.toast(activity,
											R.string.server_unavailable);
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}), isShowDialog, params);
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int arg2,
			long arg3) {
		try {
			JSONObject object = (JSONObject) view.getTag();
			boolean isDoctor = object.getInt("User_Type") == 2;
			if (isDoctor) {
				object.put("Is_Follow", 1);
				Intent intent = new Intent(activity, DoctorDetailActivity.class);
				intent.putExtra(Constant.IDENTITY_KEY, object.toString());
				intent.putExtra(Constant.IDENTITY_KEY_1,
						Constant.IDENTITY_KEY_1);
				startActivity(intent);
			} else {
				object.put("Is_Follow", 1);
				Intent intent = new Intent(activity,
						QuestionDetailActivity.class);
				intent.putExtra(Constant.IDENTITY_KEY, object.toString());
				intent.putExtra(Constant.IDENTITY_KEY_1, true);
				startActivity(intent);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	/*@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/

}
